/* 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client;

import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblog.AddBlogController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblog.AddBlogView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblog.events.in.ShowAddBlogViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblogentry.AddBlogEntryController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblogentry.AddBlogEntryModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblogentry.AddBlogEntryView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.addblogentry.events.in.ShowAddBlogEntryViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.dashboard.DashboardController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.dashboard.DashboardModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.dashboard.DashboardView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.dashboard.events.in.ShowDashboardViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.blog.EditBlogController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.blog.EditBlogModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.blog.EditBlogView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.blog.events.in.ShowEditBlogViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.userdata.EditUserDataController;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.userdata.EditUserDataModel;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.userdata.EditUserDataView;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.edit.userdata.events.in.ShowEditUserDataViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.framework.client.module.AModuleControllerRegistry;

import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.event.IEvent;

/**
 * 
 *
 * @author jens.meiss
 */
public class Dashboard extends AModuleControllerRegistry {
	
	public void handleEventImpl(IEvent event) {
	}

	public void initImpl() {
		
	}

	public void onHistoryChanged(String historyToken) {
	
		if (historyToken.equals(HistoryToken.dashboard)) {
			
			showDashboard();
			
		} else if (historyToken.equals(HistoryToken.addBlog)) {
			
			showAddBlog();
			
		} else if (historyToken.matches("^" + HistoryToken.editBlog + "=[0-9]+$")) {
			
			editBlog(Integer.parseInt(historyToken.substring(HistoryToken.editBlog.length() + 1)));
			
		} else if (historyToken.equals(HistoryToken.editUserdata)) {
			
			editUserData();
			
		} else if (historyToken.matches("^" + HistoryToken.addBlogEntry + "=[0-9]+$")) {
			
			addBlogEntry(Integer.parseInt(historyToken.substring(HistoryToken.addBlogEntry.length() + 1)));
			
		} else if (historyToken.matches("^" + HistoryToken.editBlogEntry + "=[0-9]$")) {
			
			editBlogEntry(Integer.parseInt(historyToken.substring(HistoryToken.editBlogEntry.length() + 1)));
			
		}
		
	}
	
	/**
	 * @param parseInt
	 */
	private void editBlogEntry(int parseInt) {
		
		
		
	}

	/**
	 * @param blogId
	 */
	private void addBlogEntry(int blogId) {
		
		if (!getComponent().containsChildComponent(AddBlogEntryController.class)) {
			addChildComponent(new Component(new AddBlogEntryModel(blogId), new AddBlogEntryView(), new AddBlogEntryController()));
		}
		
		fireEventDown(new ShowAddBlogEntryViewEvent());
		
	}

	/**
	 * 
	 */
	private void editUserData() {
		
		if (!getComponent().containsChildComponent(EditUserDataController.class)) {
			addChildComponent(new Component(new EditUserDataModel(), new EditUserDataView(), new EditUserDataController()));
		}
		
		fireEventDown(new ShowEditUserDataViewEvent());
		
	}

	/**
	 * @param parseLong
	 */
	private void editBlog(int blogId) {
		
		if (!getComponent().containsChildComponent(EditBlogController.class)) {
			addChildComponent(new Component(new EditBlogModel(blogId), new EditBlogView(), new EditBlogController()));
		}
		
		fireEventDown(new ShowEditBlogViewEvent(blogId));
		
	}

	private void showAddBlog() {
		
		if (!getComponent().containsChildComponent(AddBlogController.class)) {
			addChildComponent(new Component(new AddBlogView(), new AddBlogController()));
		} 		
		
		fireEventDown(new ShowAddBlogViewEvent());
	}

	private void showDashboard() {
		
		if (!getComponent().containsChildComponent(DashboardController.class)) {
			addChildComponent(new Component(new DashboardModel(), new DashboardView(), new DashboardController()));
		}
		
		fireEventDown(new ShowDashboardViewEvent());
	}

	public void terminateImpl() {
	}

	

}
